package org.leumasjaffe.math;

public class Vector {
	double[] dimms;
	double magnitude;
	int dim;
	
	public Vector(double...ns) {
		this.dimms = ns;
		this.dim = ns.length;
		calcMagnitude();
	}
	
	private void calcMagnitude() {
		double sum = 0;
		for (int i = 0; i < this.dim; i++)
			sum += this.dimms[i] * this.dimms[i];
		this.magnitude = Math.sqrt(sum);		
	}

	public void add(Vector v) {
		for (int i = 0; i < this.dim; i++)
			this.dimms[i] += v.dimms[i];
		calcMagnitude();
	}
	
	public void subtract(Vector v) {
		for (int i = 0; i < this.dim; i++)
			this.dimms[i] -= v.dimms[i];
		calcMagnitude();
	}
	
	public void scalar(double c) {
		for (int i = 0; i < this.dim; i++)
			this.dimms[i] *= c;
		this.magnitude *= c;
	}
	
	public void normalize() {
		scalar(1/this.magnitude);
	}
	
	public static Vector sum(Vector v, Vector u) {
		double[] temp = new double[v.dim];
		for (int i = 0; i < v.dim; i++)
			temp[i] = v.dimms[i] + u.dimms[i];
		return new Vector(new double[v.dim]);
	}
	
	public static Vector difference(Vector v, Vector u) {
		return sum(v, negative(u));
	}
	
	public static Vector multiply(double c, Vector v) {
		double[] temp = new double[v.dim];
		for (int i = 0; i < v.dim; i++)
			temp[i] = c*v.dimms[i];
		return new Vector(temp);
	}
	
	public static Vector negative(Vector v) {
		return multiply(-1, v);
	}
	
	public static Vector divide(double c, Vector v) {
		return multiply(1/c, v);
	}

}
